/*
 * ChatFrame.java
 *
 * Created on May 9, 2008, 7:11 PM
 */

package GameControl;

import Control.Controller;
import Network.NetworkProtocol;
import Main.Main;

/**
 *
 * @author  hell
 */
public class ChatFrame extends java.awt.Frame {
    
    /** Creates new form ChatFrame */
    public ChatFrame() {
        initComponents();
    }


    private void handleButtonPressed() {
        String intext = ChatInput.getText();
        if (intext.length() > 0){
            //ChatLog.append("<"+Controller.selfID+"> "+intext+"\n"); // Handled by looping packets
            //ChatLog.selectAll();
            //Make sure the new text is visible, even if there
            //was a selection in the text area.
            // chatlog.setCaretPosition(chatlog.getRows()*chatlog.getColumns()); // rather shitty thing
            
            ChatInput.setText(""); // Clean entered message
            
            Control.Message msg = new Control.Message(intext);
            try {
                NetworkProtocol.SendCommand(msg);
            } catch (java.io.IOException e) {
                Main.PrintDebug(8, "ChatFrame: Could not send a message");

            }
        }
    }
    
    public void AddMessage(String message){
        ChatLog.append(message+"\n"); // *! Handle scrolling of text here somehow.
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        SendButton = new java.awt.Button();
        ChatLog = new java.awt.TextArea();
        ChatInput = new java.awt.TextField();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });
        setLayout(new java.awt.GridBagLayout());

        SendButton.setLabel("Send");
        SendButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SendButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        add(SendButton, gridBagConstraints);

        ChatLog.setEditable(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(ChatLog, gridBagConstraints);

        ChatInput.setBackground(new java.awt.Color(255, 255, 255));
        ChatInput.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ChatInputActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        add(ChatInput, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
        System.exit(0);
    }//GEN-LAST:event_exitForm

    private void SendButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SendButtonActionPerformed
        handleButtonPressed();
    }//GEN-LAST:event_SendButtonActionPerformed

    private void ChatInputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ChatInputActionPerformed
        handleButtonPressed();
    }//GEN-LAST:event_ChatInputActionPerformed
    
    /**
     * @param args the command line arguments
     */
/*    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ChatFrame().setVisible(true);
            }
        });
    }*/
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.TextField ChatInput;
    private java.awt.TextArea ChatLog;
    private java.awt.Button SendButton;
    // End of variables declaration//GEN-END:variables
    
}
